import apis from '../apis/apis'
import UE from '../utils/error'

export default {
  install(Vue) {
    Vue.prototype.$apis = apis

    /**
     * 发送ajax请求
     * @author zhangkang
     * @param { Object } options
     * @tip   options.ctx指向当前VueComponent实例上下文
     */
    Vue.prototype.$ajax = function(options = UE.fnArgumentsEmptyErr('Axios ajax', 'options')) {
      UE.fixedTypeEmptyErr(options, 'object')
      options.ctx = !options.ctx ? this : UE.fixedTypeEmptyErr(options.ctx, 'object')
      options.headers = !options.headers ? { 'Content-Type': 'application/json' } : UE.fixedTypeEmptyErr(options.headers, 'object')
      return new Promise((resolve, reject) => {
        options.ctx.$axios({
          method: !options.method ? 'get' : options.method,
          url: UE.fixedTypeEmptyErr(options.url, 'string'),
          data: !options.data ? null : options.data,
          headers: options.headers
        }).then(res => {
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    }
  }
}
